package com.yj.suncity.adapter;

import java.util.List;

import com.yj.suncity.R;
import com.yj.suncity.activity.shouye.bean.ShouyeGoodsBean;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.Utils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ShouyeGoodsGalleryAdapter extends BaseAdapter {
	private Context context;
	int mWidth;
	int dip_16;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;
	private List<ShouyeGoodsBean> goodsBeans;
	private CallBackId callBackId;

	public interface CallBackId {
		void callBackId(String id);
	}

	public ShouyeGoodsGalleryAdapter(Context context,
			List<ShouyeGoodsBean> goodsBeans, CallBackId callBackId) {
		super();
		this.context = context;
		this.goodsBeans = goodsBeans;
		this.callBackId = callBackId;

		mWidth = context.getResources().getDisplayMetrics().widthPixels;
		dip_16 = Utils.dipTopx(context, 16);
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.android_layout_detail)
				.showImageForEmptyUri(R.drawable.android_layout_detail)
				.showImageOnFail(R.drawable.android_layout_detail)
				.cacheInMemory(true).cacheOnDisc(true)
				// .displayer(new RoundedBitmapDisplayer(10))// 设置成圆角图片
				.bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
				.build();
	}

	@Override
	public int getCount() {
		return goodsBeans.size()==1?2:goodsBeans.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.adapter_gallery_item, null);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.img_logo);
			holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(
					(int) ((mWidth - dip_16) / 3.2),
					(int) ((mWidth - dip_16) / 3.2)));
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ShouyeGoodsBean goodsBean;
		if (goodsBeans.size() > 0) {
			goodsBean = goodsBeans.get(position % goodsBeans.size());
		} else {
			return convertView;
		}
		
		if (position==1&&goodsBeans.size()==1) {
			
		} else {
			if (!goodsBean.getImg_url().equals("")) {
				if (goodsBean.getImg_url().contains(",")) {
					imageLoader.displayImage(URLCon.HOST + (goodsBean.getImg_url().split(","))[0],
							holder.imageView, options);
				} else {
					imageLoader.displayImage(URLCon.HOST + goodsBean.getImg_url(),
							holder.imageView, options);
				}
			} else {
				holder.imageView.setImageResource(R.drawable.android_layout_detail);
			}
			holder.imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					callBackId.callBackId(goodsBean.getId());
				}
			});
		}


		return convertView;
	}

	class ViewHolder {
		ImageView imageView;
	}
}
